import {
  config
} from "../../config"
let timer;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    codeImageList:[
      {
        id:0,
        ico:'https://h5-touch.oss-cn-shanghai.aliyuncs.com/project/intel/image/code/location-ico1.png',
        locationImage:'https://h5-touch.oss-cn-shanghai.aliyuncs.com/project/intel/image/code/location-image1.png'
      },
      {
        id:1,
        ico:'https://h5-touch.oss-cn-shanghai.aliyuncs.com/project/intel/image/code/location-ico2.png',
        locationImage:'https://h5-touch.oss-cn-shanghai.aliyuncs.com/project/intel/image/code/location-image2.png'
      },
      {
        id:2,
        ico:'https://h5-touch.oss-cn-shanghai.aliyuncs.com/project/intel/image/code/location-ico3.png',
        locationImage:'https://h5-touch.oss-cn-shanghai.aliyuncs.com/project/intel/image/code/location-image3.png'
      },
      {
        id:3,
        ico:'https://h5-touch.oss-cn-shanghai.aliyuncs.com/project/intel/image/code/location-ico4.png',
        locationImage:'https://h5-touch.oss-cn-shanghai.aliyuncs.com/project/intel/image/code/location-image4.png'
      },
    ],
    codeIndex:0,//当前关卡
    codeImageUrl:'',//生产的二维码
  },
  return(){
    clearInterval(timer);
    // wx.redirectTo({
    //   url: '../map/index'
    // })
    wx.navigateBack({
      delta: 1  // 返回上一级页面。
    })
  },
  drawCode(codeIndex){
   const openId = wx.getStorageSync('openid')
   // const openId = '1111cccssss'
    const index = codeIndex+1
    const testData = `openid=${openId}&g${index}=1`
    console.log(testData)
    this.setData({
      codeImageUrl:`https://api.shanghaichujie.com/api/qrcode/generate?text=${escape(testData)}`
    })
    timer = setInterval(() => {
      this.getUserData()
    },1500);
  },
  getUserData() {
    const openId = wx.getStorageSync('openid')
    //const openId = '1111cccssss'
    const index = parseInt(this.data.codeIndex)+1
    wx.request({
      url: `${config.api_blink_url}/Intel/g/user?openid=${openId}`,
      method: "get",
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
       const gIndex = `g${index}`
       if(res.data.user[gIndex]===1){
        wx.redirectTo({
          url: '../map/index'
        })
        clearInterval(timer);
       }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { 
    this.setData({
      codeIndex:Number(options.round)
    })
    this.drawCode(this.data.codeIndex)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})